import tkinter as tk
from PIL import Image, ImageTk
from tkinter import messagebox

class CultivationInterface:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title("修仙界面 v4.1")
        self.root.geometry("1060x708")
        self.root.resizable(False, False)

        # 初始化背景图
        self.setup_background()

        # 初始化界面元素
        self.setup_character()
        self.setup_map()
        self.setup_status_panel()

        self.root.mainloop()

    def setup_background(self):
        """设置全屏背景图"""
        # 加载并调整背景图大小
        background_image = Image.open("F:/python2048/images/大背景.jpg")
        background_image = background_image.resize((1060, 708), Image.Resampling.LANCZOS)
        self.background_photo = ImageTk.PhotoImage(background_image)

        # 创建用于显示背景图片的Label
        self.background_label = tk.Label(self.root, image=self.background_photo)
        self.background_label.place(x=0, y=0, relwidth=1, relheight=1)

    def setup_character(self):
        """左侧人物立绘系统（精确高度控制）"""
        # 创建左侧框架
        left_frame = tk.Frame(self.root, width=360, height=708)
        left_frame.place(x=0, y=0)

        # 加载并调整人物立绘图像大小
        img = Image.open("F:/python2048/男主角1.jpg")
        max_width = 280  # 人物立绘的最大宽度
        max_height = 440  # 保留底部空间
        ratio = min(max_width / img.width, max_height / img.height)
        new_size = (int(img.width * ratio), int(img.height * ratio))
        img = img.resize(new_size, Image.Resampling.LANCZOS)
        self.char_img = ImageTk.PhotoImage(img)

        # 创建Canvas并显示人物立绘
        canvas = tk.Canvas(left_frame, width=max_width, height=max_height, highlightthickness=0)
        canvas.pack(side="bottom", pady=20)
        canvas.create_image(max_width // 2, max_height, image=self.char_img, anchor="s")

    def setup_map(self):
        """中间地图系统（带精确边距和可点击的位置）"""
        # 创建中间框架
        center_frame = tk.Frame(self.root, width=560, height=708)
        center_frame.place(x=350, y=0)

        # 加载并调整地图图像大小
        img = Image.open("F:/python2048/地图.jpg")
        img.thumbnail((500, 650))  # 限制最大尺寸
        self.map_img = ImageTk.PhotoImage(img)

        # 创建Canvas并显示地图
        self.map_canvas = tk.Canvas(center_frame, width=self.map_img.width(), height=self.map_img.height(), highlightthickness=0)
        self.map_canvas.pack(expand=True)
        self.map_canvas.create_image(0, 0, anchor="nw", image=self.map_img)

        # 添加可点击的位置
        locations = [
            ("杨家口", 114, 239),
            ("苍灵峰", 330, 140),
            ("天玄阁", 270, 110),
            ("书阁", 220, 190),
            ("逍遥门", 220, 297),
            ("白家寨子", 265, 235),
            ("黄鹤楼", 225, 145),
            ("灵河宗", 300, 360),
            ("轩村", 249, 485),
            ("左家城", 215, 389),
            ("两仪镇", 81, 370),
            ("丹阁", 61, 335),
            ("木灵宗", 321, 285),
            ("渔村", 341, 205)
        ]

        for name, x, y in locations:
            self.map_canvas.create_oval(x - 5, y - 5, x + 5, y + 5, fill="red", tags=name)
            self.map_canvas.tag_bind(name, "<Button-1>", lambda e, n=name: self.location_click(n))

    def location_click(self, name):
        messagebox.showinfo("位置", f"你进入了{name}!")

    def setup_status_panel(self):
        """右侧状态面板（显式按钮布局）"""
        # 创建右侧框架
        right_frame = tk.Frame(self.root, width=150, height=708)
        right_frame.place(x=910, y=0)

        # 状态数据区
        data_frame = tk.Frame(right_frame, padx=5, pady=5, bg="#000000")
        data_frame.pack(fill="x")

        status_items = [
            ("境界", "金丹后期"),
            ("修为", "■■■■□ 80%"),
            ("气血", "12800/15000"),
            ("真气", "8800/10000")
        ]

        for text, value in status_items:
            row = tk.Frame(data_frame, bg="#000000")
            row.pack(fill="x", pady=5)

            tk.Label(row, text=text, font=("微软雅黑", 11), fg="#AAAAAA", bg="#000000", width=6, anchor="e").pack(side="left")
            tk.Label(row, text=value, font=("微软雅黑", 11, "bold"), fg="#FFFFFF", bg="#000000").pack(side="left")

        btn_frame = tk.Frame(right_frame, padx=5, pady=10, bg="#000000")  # 设置按钮区背景为黑色
        btn_frame.pack(fill="both", expand=True)

        buttons = [
            ("修 炼", "#4CAF50", self.cultivation),
            ("炼 丹", "#FF9800", self.alchemy),
            ("炼 器", "#2196F3", self.forging),
            ("秘 境", "#E91E63", self.secret_realm)
        ]

        for text, color, command in buttons:
            btn = tk.Button(btn_frame, text=text, font=("微软雅黑", 14), bg=color, fg="white", height=2, relief="raised", borderwidth=3, command=command)
            btn.pack(fill="x", pady=5)

    def cultivation(self):
        messagebox.showinfo("修炼", "你开始了闭关修炼，修为有所提升！")

    def alchemy(self):
        messagebox.showinfo("炼丹", "你开始炼制丹药，获得了一些珍贵的丹药！")

    def forging(self):
        messagebox.showinfo("炼器", "你开始锻造法器，打造出了一件不错的法器！")

    def secret_realm(self):
        messagebox.showinfo("秘境", "你进入了一处神秘的秘境，发现了许多宝藏！")

if __name__ == "__main__":
    app = CultivationInterface()

